package com.bocai.yuweiwangluo.utils;

import android.app.Activity;
import android.graphics.drawable.BitmapDrawable;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.PopupWindow;

import com.bocai.yuweiwangluo.R;
import com.bocai.yuweiwangluo.adapter.PopWindowAdapter;
import com.bocai.yuweiwangluo.bean.PopBean;

import java.util.ArrayList;

/**
 * Created by admin on 2015/12/16.
 */
public class PopUtil
{
    private OnDatePopClickListener listener;
    private static PopUtil mInstance;
    private Activity mContext;
    private PopupWindow pop;
    private ListView poplv;
    private View showBottomView;
    private int width;
    private int height;
    public static ArrayList<PopBean> popBeans;
    private PopUtil(Activity context,View view,int width,int height)
    {
        this.mContext = context;
        this.showBottomView=view;
        this.width=width;
        this.height=height;
        init();
    }

    public static PopUtil   getInstance(Activity context,View view,int width,int height,ArrayList<PopBean> list)
    {
        popBeans=list;
        return mInstance == null ? new PopUtil(context,view,width,height) : mInstance;
    }
    private void init()
    {
        View view = initView(getView());

        createPop(view);
    }
    private View getView()
    {
        return LayoutInflater.from(mContext).inflate(R.layout.popwindow_list, null);
    }
    private View initView(View view)
    {
        View popview=LayoutInflater.from(mContext).inflate(R.layout.popwindow_list,null);
        poplv=(ListView)popview.findViewById(R.id.lv_popwindow);
        PopWindowAdapter popWindowAdapter=new PopWindowAdapter(mContext,popBeans);
        poplv.setAdapter(popWindowAdapter);
        poplv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                if (listener != null) {
                    listener.onClick(PopUtil.this, (PopBean) parent.getAdapter().getItem(position));
                }
            }
        });
        return popview;
    }
    private void createPop(View view)
    {
//        if(popBeans.size()*100<height)
//        {
//            pop=new PopupWindow(view,width,popBeans.size()*100);
//        }else
//        {
            pop=new PopupWindow(view,width,height);
//        }
        pop.setFocusable(true);
        pop.setOutsideTouchable(true);
        // 这个是为了点击“返回Back”也能使其消失，并且并不会影响你的背景
        pop.setBackgroundDrawable(new BitmapDrawable());
    }
    public void show()
    {
        if (!pop.isShowing())
        {
            pop.showAsDropDown(showBottomView, 0, 0);
        }
    }

    public void dismiss()
    {
        if (pop.isShowing())
        {
            pop.dismiss();
        }
    }
    /**
     * 方法说明：设置确定、取消按钮的监听器
     *
     * @param listener
     */
    public void setListener(OnDatePopClickListener listener)
    {
        this.listener = listener;
    }

    /**
     * 一个简单的回调监听
     */
    public interface OnDatePopClickListener
    {
        void onClick(PopUtil pu, PopBean date);
    }
}
